// pages/commentSubmit/commentSubmit.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    requestData:{},//评论提交数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(JSON.parse(options.item))
    let item = JSON.parse(options.item);
    //组装请求数据
    var orderItems = item.orderItems;
    var newOrderItems = [];
    orderItems.forEach((n, i) => {
      newOrderItems.push({
        goodsImage: n.image,
        goodsTitle: n.title,
        orderItemId: n.id,
        describeStart: 0,
        logisticsStart: 0,
        serviceStart: 0,
        content: '',
        showImgs: [],
        isAnonymous: true
      });
    });
    var data = {
      "orderId": item.order.id,
      "commentItems": newOrderItems
    };
    this.setData({
      requestData: data
    })
    console.log(this.data.requestData);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    app.globalData.otherPageHideFlag = true;
  },
  submitComment(){
    var _this = this;
    console.log(this.data.requestData);
    Api.submitComment(this.data.requestData).then(res=>{
      console.log(res);
      //跳转订单页面
      wx.showToast({
        icon:'none',
        title: '评论成功',
        duration: 1500
      })
      setTimeout(()=>{
        wx.redirectTo({
          url: '/pages/seeOrder/seeOrder?orderStatusIndex=5',
        })
      },'1500')
    });
  },
  /**
   * 选择图片的方法
   */
  choose(e) {
    let index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;
    let that = this;  
    var images = that.data.requestData.commentItems[index].showImgs;
    if (images >= 5) {
      wx.showToast({
        title: '最多选择5张！',
        icon: 'none'
      })
      return;
    }
    let prics = that.data.imgList;
    wx.chooseImage({           
      count: 1,
        // 默认9，这里显示一次选择相册的图片数量          
       sizeType:  ['original',  'compressed'], // 可以指定是原图还是压缩图，默认二者都有         
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有      
        success: function(res)  { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        console.log(res)          
        let tempFiles = res.tempFiles[0].path;                 //把选择的图片 添加到集合里       
        wx.uploadFile({                   
          url: app.globalData.Prefix + '/common/file/upload',
            // 自己的服务器提供的图片的小程序choose接口地址                
          filePath: tempFiles,
             name: 'file',
          success(res)  {                        
            console.log(res, "请求上传服务器的接口");    
            res = JSON.parse(res.data);
            if (res.flag){
              images.push(res.data);
              that.updateCommentList(index, name, images);
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
              })
            }
          }                
        })            
      }        
    })
  },
  /**
   * 去除图片事件
   */
  imgClose(e) {
    var requestData = this.data.requestData;
    var commentItems = requestData.commentItems;
    var commentIndex = e.currentTarget.dataset.commentindex;
    var item = commentItems[commentIndex];
    var showImgs = item.showImgs;
    var index = e.currentTarget.dataset.index;
    showImgs.splice(index, 1);
    this.setData({
      requestData: requestData
    });
  },
  update(e){
    console.log(e);
    let index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;
    let value = e.currentTarget.dataset.value;
    if (e.detail.value){
      console.log('输入框', e.detail.value);
      value = e.detail.value;
    }
    this.updateCommentList(index,name,value);
  },
  //更新数组数据
  updateCommentList(index,name,value){
    console.log(index, name, value);
    var requestData = this.data.requestData;
    var commentItems = requestData.commentItems;
    var item = commentItems[index];
    for (var key in item){
      if (key === name){
        item[key] = value;
        break;
      }
    }
    this.setData({
      requestData: requestData
    });
    console.log(this.data.requestData);
  }
})